package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/8 20:10
 * https://leetcode-cn.com/problems/diagonal-traverse/
 */
public class Title498 {

    public static int[] findDiagonalOrder(int[][] mat) {
        int n = mat.length;
        int m = mat[0].length;
        int[] result = new int[n * m];
        int count = 0;
        int i = 0, j = 0;
        while (count < n * m) {
            //向上走
            if ((i + j) % 2 == 0) {
                while (i >= 0 && j < m) {
                    result[count++] = mat[i--][j++];
                }
                if (j == m) {
                    i = i + 2;
                    j = j - 1;
                } else {
                    i = i + 1;
                }
            } else {
                //向下走
                while (i < n && j >= 0) {
                    result[count++] = mat[i++][j--];
                }
                if (i == n) {
                    i = i - 1;
                    j = j + 2;
                } else {
                    j = j + 1;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        findDiagonalOrder(mat);
    }
}
